package midlet;

import midlet.midlet;
import java.util.Random;
import javax.microedition.lcdui.*;
public class Blocks extends Canvas implements Runnable,CommandListener
{
	int x[],y[],mx[],my[],p,q,i,a,k,fullcurrent,h,w;
        boolean closed=false;
	Image img;
	Random rnd;
	Form debug;
	Display d;
	midlet main;
	Thread t;
	
	TextField imagename=new TextField("Image Name","tile.png",50,0); 
	Gauge image=new Gauge("Image",true,1,1);
	  Gauge clrscr=new Gauge("Clear Screen",true,1,0);
	  Gauge bounds=new Gauge("Image Bound",true,1,1);
	  Gauge noblocks=new Gauge("No. of Blocks",true,30,3);
	  Gauge delay=new Gauge("Delay",true,50,1);
	  Gauge speed=new Gauge("Speed",true,50,1);
	  Gauge priority=new Gauge("Priority",true,10,10);
	  Gauge width=new Gauge("Width of Blocks",true,100,30);
	  Gauge height=new Gauge("Height of Blocks",true,100,30);
	  Gauge softness=new Gauge("Softness of Blocks",true,50,5);
	  Gauge angle=new Gauge("Angle of Softness",true,50,15);
	  Gauge animation=new Gauge("Animation",true,1,1);
	  Gauge collision=new Gauge("Collision-Detection",true,1,1);
	Command Enter= new Command("Enter",Command.BACK,0);
	Command CanvasOptions=new Command("Options",Command.SCREEN,3);
	Command mainscreen= new Command("Main",Command.SCREEN,1);
	Command Full=new Command("FullScreen",Command.SCREEN,3);
	public Form canvasoptions;
	Blocks(Form debuginfo,Display disp,midlet all)
	{d=disp;
	fullcurrent=1;
        this.setFullScreenMode(true);
		this.addCommand(CanvasOptions);
		this.addCommand(mainscreen);
		this.addCommand(Full);
	main=all;
	this.setCommandListener(this);
	canvasoptions=new Form("Options");
	canvasoptions.addCommand(Enter);
	  canvasoptions.setCommandListener(this);
	  canvasoptions.append(imagename);
	  canvasoptions.append(animation);
	  canvasoptions.append(image);
	  canvasoptions.append(noblocks);
	  canvasoptions.append(delay);
	  canvasoptions.append(priority);
	  canvasoptions.append(speed);
	  canvasoptions.append(collision);
	  canvasoptions.append(width);
	  canvasoptions.append(height);
	  canvasoptions.append(softness);
	  canvasoptions.append(angle);
	  canvasoptions.append(clrscr);
	  canvasoptions.append(bounds);
	 
		rnd=new Random();	
	debug=debuginfo;
	p=30;q=30;
	rnd.setSeed(noblocks.getValue());
	x=new int[noblocks.getValue()];
	y=new int[noblocks.getValue()];
	mx=new int[noblocks.getValue()];
	my=new int[noblocks.getValue()];
	for(i=0;i<noblocks.getValue();i++)
	{x[i]=rnd.nextInt();
	y[i]=(i*rnd.nextInt())%320;
	}
	i=1;
try {img=Image.createImage(imagename.getString());}
catch (Exception e) {
	e.printStackTrace();
	debug.append(e.getMessage());
}
t=new Thread(this);
t.start();
t.setPriority(10);

	}
	protected void keyPressed(int key)
	{k=key;repaint();}
	protected void keyReleased(int key)
	{k=0;repaint();}
	protected void keyRepeated(int key)
	{k=key;repaint();}
	protected void pointerPressed(int p1,int p2)
	{x[i]=p1;y[i]=p2;repaint();}
	protected void pointerReleased(int p1,int p2)
	{x[i]=p1;y[i]=p2;repaint();}
	protected void pointerDragged(int p1,int p2)
	{x[i]=p1;y[i]=p2;repaint();}
	public void run()
	{while(!closed)
		{repaint();
		try{
			synchronized(this)
			{wait(delay.getValue()*(1L));}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			debug.append(e.getMessage());
		}
		}
		
		}
		void collisiontest()
		{
			for(a=0;a<noblocks.getValue();a++)
			{if(x[i]>x[a]&&x[i]<x[a]+width.getValue()&&y[i]>y[a]&&y[i]<y[a]+height.getValue()){
				if(x[i]>x[a]&&x[i]<x[a]+width.getValue())
				{if(i!=a){
					if(x[a]>x[i]){x[a]+=(x[a]-x[i])/2;x[i]-=(x[a]-x[i])/2;}
					else if(x[a]<x[i]){x[a]-=(x[i]-x[a])/2;x[i]+=(x[i]-x[a])/2;}
					if(mx[i]!=mx[a]){mx[i]=(mx[i]+1)%2;
					mx[a]=(mx[a]+1)%2;}
					}}
				if(y[i]>y[a]&&y[i]<y[a]+height.getValue())
				{if(i!=a){
					if(y[a]>y[i]){y[a]+=(y[a]-y[i])/2;y[i]-=(y[a]-y[i])/2;}
					else if(y[a]<y[i]){y[a]-=(y[i]-y[a])/2;y[i]+=(y[i]-y[a])/2;}
					if(my[i]!=my[a]){my[i]=(my[i]+1)%2;
					my[a]=(my[a]+1)%2;}
					}}	
			}}
			
		}
		void motion()
		{
			if(mx[i]==1)x[i]+=speed.getValue();
			else x[i]-=speed.getValue()+1;
			if(my[i]==1)y[i]+=speed.getValue();
			else y[i]-=speed.getValue();
		}
		void key()
		{
			if(k==-1)q-=speed.getValue();
			else if(k==-2)q+=speed.getValue();
			else if(k==-3)p-=speed.getValue();
			else if(k==-4)p+=speed.getValue();
			
		}
	protected void paint(Graphics g) {
		h=g.getClipHeight();
		w=g.getClipWidth();
		i++;
		if(i>=noblocks.getValue())i=0;
		if(clrscr.getValue()==1){g.setColor(255, 255, 255);
		g.fillRoundRect(0,0,w,h,softness.getValue(),angle.getValue());}
		else if(animation.getValue()==1){g.setColor(255, 255, 255);
		g.fillRoundRect(x[i],y[i],width.getValue(),height.getValue(),softness.getValue(),angle.getValue());}
		if(bounds.getValue()==1)
		{
			if(p<0)p=0;
			if(q<0)q=0;
			if(p>w)p=w;
			if(q>h)q=h;
		}
		if((img!=null)&&(image.getValue()==1))g.drawImage(img,p,q,1|2);
		key();
		if(collision.getValue()==1)collisiontest();
		motion();
		if(x[i]<0){mx[i]=1;x[i]=0;}
		if(y[i]<0){my[i]=1;y[i]=0;}
		if(x[i]>w-width.getValue()){mx[i]=0;x[i]=w-width.getValue();}
		if(y[i]>h-height.getValue()){my[i]=0;y[i]=h-height.getValue();}
		g.setColor(((i+1)*x[i])%255,((i+1)*y[i])%255,((i+1)*(x[i]+y[i]))%255);
		g.fillRoundRect(x[i],y[i],width.getValue(),height.getValue(),softness.getValue(),angle.getValue());
	}
	public void commandAction(Command c, Displayable disp) {
	if(c==CanvasOptions)
	{
		d.setCurrent(canvasoptions);
	}
	else if(c==Enter)
	{t.setPriority(priority.getValue());
	x=new int[noblocks.getValue()];
	y=new int[noblocks.getValue()];
	mx=new int[noblocks.getValue()];
	my=new int[noblocks.getValue()];
	try {img=Image.createImage(imagename.getString());
	}
	catch (Exception e) {
		e.printStackTrace();
		debug.append(e.getMessage());
	}
	for(i=0;i<noblocks.getValue();i++)
	{x[i]=(i*rnd.nextInt())%w;
	y[i]=(i*rnd.nextInt())%h;
	}
		d.setCurrent(this);
	}
	else if(c==mainscreen)
	{closed=true;
            this.setCommandListener(main);
		d.setCurrent(main.mainmenu);
		
	}
	  else if(c==Full){
		  if(fullcurrent==0){this.setFullScreenMode(true);fullcurrent=1;}
		  else{this.setFullScreenMode(false);fullcurrent=0;}
  }
		
	}	
}